<template>
	<!-- 资料集中营首页 -->
	<view class="body">
		<myHeader :title="title"></myHeader>
		<!-- header -->
		<view class="top">
			<view class="item">
				<uni-icons class="item" type="back" @tap="back()" size="30" color="red"></uni-icons>
			</view>
			<view class="item">
				<button class="font-size-30" @click="openInsert">添加目录</button>
			</view>
			<view class="item">
				<button class="font-size-30" @click="openUpload">上传文件</button>
			</view>
		</view>
		<!-- 目录列表 -->
		<view class="mt-20">
			<view class="ml-20" style="height: 60upx;font-weight: bold;">{{curPath}}</view>
			<uni-swipe-action v-for="(item,index) in list" :key="index">
				<uni-swipe-action-item :right-options="options" @click="swipeClick($event, item.id)">
					<view class="content-box" @click="clickDir(item,index)"><text>{{item}}</text></view>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</view>


		<!-- 文件列表 -->
		<uni-grid :column="4" class="mt-40">
			<uni-grid-item>
				<text class="text">文本</text>
			</uni-grid-item>
			<uni-grid-item>
				<text class="text">文本</text>
			</uni-grid-item>
			<uni-grid-item>
				<text class="text">文本</text>
			</uni-grid-item>
			<uni-grid-item>
				<text class="text">文本</text>
			</uni-grid-item>
			<uni-grid-item>
				<text class="text">文本</text>
			</uni-grid-item>
		</uni-grid>
		<!-- 新增目录 -->
		<uni-popup ref="insert" type="center" :animation="false">
			<view class="win-insert p-20">
				<view class="content">
					<uni-forms-item label="目录名称" name="name">
						<uni-easyinput type="text" v-model="insertContentName" placeholder="请输入目录名称" />
					</uni-forms-item>
				</view>
				<view class="opt">
					<button class="item font-size-30">确认</button>
					<button class="item font-size-30" @click="closeInsert">取消</button>
				</view>
			</view>
		</uni-popup>

		<!-- 上传文件 -->
		<uni-popup ref="uploadFileWin" type="center" :animation="false">
			<view class="win-insert p-20">
				<view class="content">
					<uni-file-picker limit="1" :auto-upload="true" mode="list" file-mediatype="all" @select="select">
					</uni-file-picker>
					<progress v-if="progressVal != 0" activeColor="#4CAF50" :percent="progressVal"
						stroke-width="10"></progress>
				</view>
				<hr class="mt-20" style="background-color: #E5E5E5;height: 1px;border:none">
				<view class="mt-20">
					<button class="font-size-30" @click="closeUpload">关闭</button>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import myHeader from '../components/head.vue'
	import NetWorkTool from '@/api/netWorkTool'
	export default {
		data() {
			return {
				title: ' 资料集中营',
				// 当前用户角色
				currentRoleCode: [],
				list: [],
				// 新增目录名称
				insertContentName: "",
				// 文件上传后的url
				fileUrl: '',
				progressVal: 0,
				// 滑块
				options: [{
					text: '删除',
					style: {
						backgroundColor: 'rgb(254,156,1)'
					}
				}],

				/**** 文件渲染 ****/
				// 所有文件列表
				totalDir: [],
				// 当前文件列表
				curDir: [],
				// 当前目录索引
				curIndex: 0,
				curPath: '/index',
				// 当前目录对应的文件
				curFiles: []

			}

		},
		created() {
			this.initDir();
		},
		methods: {
			initDir() {
				NetWorkTool.POST('/api/information-camp/getPath', {}, false).then(res => {
					console.log(res)
					this.totalDir = res
					this.curDir = res
					for (let item of res) {
						console.log(item.split('/'))
						this.list.push(item.split('/')[0])
					}


				});
			},
			getDir() {
				let param = {
					"path": "村支部",
					"type": 0
				}
			},
			getDirFile(url) {
				let param = {
					"path": "村支部",
					"type": 0
				}
				NetWorkTool.POST('/api/information-camp/getPath', {}, false).then(res => {

				});

			},
			clickDir(item, index) {
				console.log("+++")
				this.curDir =  this.curDir[index]
				console.log(this.curDir)

			},
			// 文件上传
			// 获取上传状态
			select(e) {
				let source = e.tempFiles[0];
				let token = '';
				uni.getStorage({
					key: 'AuthToken',
					success(e) {
						token = e
					}
				})
				// const uploadTask = uni.uploadFile({
				// 	url: '/api/uploadFile', //仅为示例，非真实的接口地址
				// 	filePath: e.tempFilePaths[0],
				// 	name: 'file',
				// 	header: {
				// 		'x-access-token': token.data
				// 	},
				// 	formData: {
				// 		'name': source.name,
				// 		'size': source.size
				// 	},
				// 	success: (res) => {
				// 		this.fileUrl = JSON.parse(res.data).data.url;
				// 	}
				// });
				uploadTask.onProgressUpdate((res) => {
					this.progressVal = res.progress;
					this.fileUrl = res.url;
				});
			},
			swipeClick(e, index) {
				console.log(e)
				let {
					content
				} = e;
				uni.showModal({
					title: '提示',
					content: '是否删除',
					success: res => {
						if (res.confirm) {
							this.list.splice(index, 1);
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			// 获取登录用户角色
			getLoginUserRoles() {
				let _this = this;
				uni.getStorage({
					key: 'user',
					success(e) {
						let arr = [];
						for (let item of e.data.role) {
							if (null != item.roleCode) {
								_this.currentRoleCode.push(item.roleCode)
								if (item.roleCode == 'fl-leader') {
									_this.isLeader = true;
								}
							}
						}
					}
				})
			},
			pageTo(val) {
				uni.navigateTo({
					url: val
				})
			},
			back: function() {
				uni.navigateBack();
			},
			openInsert() {
				this.$refs.insert.open('top')
			},
			openUpload() {
				this.$refs.uploadFileWin.open('top')
			},
			closeInsert() {
				this.$refs.insert.close()
			},
			closeUpload() {
				this.$refs.uploadFileWin.close()
			}

		},

		components: {
			myHeader
		},
	}
</script>

<style lang="css" scoped>
	@import url("/static/party_building/assets/style/federation.css");


	.font-size-30 {
		font-size: 30upx;
	}

	.top {
		display: flex;
		justify-content: space-around;
		align-items: center;
	}

	.top .item {
		flex: 0 0 33%;
	}

	.win-insert {
		min-width: 600upx;
		background-color: #FFFFFF;
		border: 1px solid #FFFFFF;
		border-radius: 8px;
	}

	.win-insert .content {
		height: 80%;

	}

	.win-insert .opt {
		height: 20%;
		display: flex;
		justify-content: space-around;
	}

	.win-insert .opt .item {
		flex: 0 0 50%;
	}

	.content-box {
		flex: 1;
		height: 44px;
		line-height: 44px;
		padding: 0 15px;
		position: relative;
		background-color: #fff;
		border: 1px solid #f5f5f5;
	}
</style>
